package com.ksd.quartz.http;


import com.ksd.common.core.crypto.SHA1;
import com.ksd.common.utils.sign.BytesUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

public class DRMHttpHeader {
    private final static String AUTHORIZATION_KEY = "wHL482gqnBarlSdZ";

    public final static String Authorization_api = "VXBhJK8x104xV_dh";

    public final static String authorization_enc = "gF11NQe9hMQ3e0vH01d93746b5b02dbf1ff93ab4da2e6b447c7107db";
    public final static String appId_enc = "9Lt72o69bRO0b9Jd";
    private String appId;
    private String Authorization;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAuthorization() {
        return Authorization;
    }

    public void setAuthorization(String authorization) {
        Authorization = authorization;
    }

    public DRMHttpHeader(HttpServletRequest request) {
        appId = request.getHeader("appId");
        Authorization = request.getHeader("Authorization");
    }

    public boolean checkAuthorization() {
        if(this.Authorization == null || this.Authorization.length() != 56) {
            return false;
        }
        String nonce = this.Authorization.substring(0, 16);
        String checkData = this.Authorization.substring(16, this.Authorization.length());

        byte[] sha1 = null;
        try {
            sha1 = SHA1.getSha1((nonce + AUTHORIZATION_KEY).getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        if(BytesUtils.ByteToHexString(sha1).equals(checkData)) {
            return true;
        }
        return false;
    }


    public boolean checkAuthorizationByAppKey(String appKey) {

        if(this.Authorization == null || this.Authorization.length() != 56) {
            return false;
        }
        if (appKey == null || appKey.length() != 16){
            return false;
        }
        String nonce = this.Authorization.substring(0, 16);
        String checkData = this.Authorization.substring(16, this.Authorization.length());

        byte[] sha1 = null;
        try {
            sha1 = SHA1.getSha1((nonce + appKey).getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        if(BytesUtils.ByteToHexString(sha1).equals(checkData)) {
            return true;
        }
        return false;
    }

    public static String getAuth(String appKey,String nonce){

            //鉴权参数生成算法为16字节的随机数+HEX_String(SHA1(16字节的随机数+接口申请人申请的appKey))
            //如：随机数为“gF11NQe9hMQ3e0vH”，接口申请人申请的appId为“9Lt72o69bRO0b9Jd”，appKey为“VWaJKP371M421g6c”，则鉴权参数为：gF11NQe9hMQ3e0vH01d93746b5b02dbf1ff93ab4da2e6b447c7107db
            byte[] sha1 = null;
            try {
                sha1 = SHA1.getSha1((nonce + appKey).getBytes("utf-8"));
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            String auth = nonce+ BytesUtils.ByteToHexString(sha1);
            return auth;
    }

    public static void main(String[] args) {
        boolean temp1 = checkAuthorizationByAppKeyAndAuth("VWaJKP371M421g6c",authorization_enc);
        System.out.println(temp1);
        //鉴权参数生成算法为16字节的随机数+HEX_String(SHA1(16字节的随机数+接口申请人申请的appKey))
        //如：随机数为“gF11NQe9hMQ3e0vH”，接口申请人申请的appId为“9Lt72o69bRO0b9Jd”，appKey为“VWaJKP371M421g6c”，则鉴权参数为：gF11NQe9hMQ3e0vH01d93746b5b02dbf1ff93ab4da2e6b447c7107db
        //        String appId = "d1S5v657UF08nQ6i";
       // String appKey = "wm17r3L4JX194673";
        String appKey = "9pv7o8FdMZgAWZP3";
        String nonce = "gF11NQe9hMQ3e0vH";//随机数
        String authStr = getAuth(appKey, nonce);
        System.out.println("生成鉴权参数: "+authStr);

        // authStr = "gF11NQe9hMQ3e0vHfefa463c52d5ce8ca0e4e5f9ed254ed7ef489f3e";
        boolean b = checkAuthorizationByAppKeyAndAuth(appKey,authStr);
        System.out.println("校验结果: "+b);
    }
    public static boolean checkAuthorizationByAppKeyAndAuth(String appKey, String authorization) {


        if(authorization == null || authorization.length() != 56) {
            return false;
        }
        if (appKey == null || appKey.length() != 16){
            return false;
        }
        String nonce = authorization.substring(0, 16);
        String checkData = authorization.substring(16, authorization.length());
        byte[] sha1 = null;
        try {
            sha1 = SHA1.getSha1((nonce + appKey).getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        if(BytesUtils.ByteToHexString(sha1).equals(checkData)) {
            return true;
        }
        return false;
    }

}